const Promise = require('bluebird');

/**
 * @api {get} /api/public/v1/articles/browse 文章列表
 */
exports.browse = function ({query}, {models: {Articles}, controller}) {
    query.status = 'Active';
    return Articles.findAndCountAll(controller.pagination({
        params: query,
        filters: ['type', 'status'],
        likes: ['slug', 'title', 'keywords'],
        attributes: ['id', 'type', 'slug', 'title', 'keywords', 'description', 'image', 'banner', 'feature', 'collect_count', 'created_at'],
        order: [['order_by', 'asc'], ['id', 'asc']]
    }));
}

/**
 * @api {get} /api/public/v1/articles/load 文章详情
 */
exports.load = function ({query: {id, slug}}, {models: {Articles}}) {
    if (id) {
        return Articles.findOne({where: {id: id}});
    }
    if (slug) {
        return Articles.findOne({where: {slug: slug}});
    }
    return {};
}